package org.swfer.UIC 
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	
	/**
	 * 按钮
	 * 可选部件 label
	 * 可选状态 over=frame2,down=last frame
	 * 它没有什特别的地方。只是多了一个修改label属性的功能。
	 * 如果SimpleButton对你来说够用了。那么你不比如此麻烦的使用这个对象。
	 * 状态视图状态时要求是不严格的。如果你只有个状态也是可以的。如果你两个状态，那控件认为是[normal] [down]
	 * @author swfer
	 */
	public class Button extends Component 
	{
		
		public function Button(view:MovieClip)
		{
			super(view);
		}
		
		///////////////////////////////////
		// methods
		///////////////////////////////////
		
		override protected function init():void
		{
			view.mouseChildren = false;
			view.buttonMode = true;
		}
		
		override protected function addEvt():void
		{	
			if (total == 1) return;
			
			view.addEventListener(MouseEvent.MOUSE_UP,onMouseEvent);
			view.addEventListener(MouseEvent.MOUSE_OUT,onMouseEvent);
			
			var total:int = view.totalFrames;
			
			if (total == 2)
			{
				view.addEventListener(MouseEvent.MOUSE_DOWN,onMouseEvent);
			}
			else if (total >= 3)
			{
				view.addEventListener(MouseEvent.MOUSE_OVER, onMouseEvent);
				view.addEventListener(MouseEvent.MOUSE_DOWN, onMouseEvent);
			}
		}
		
		override protected function removeEvt():void
		{
			view.removeEventListener(MouseEvent.MOUSE_UP,onMouseEvent);
			view.removeEventListener(MouseEvent.MOUSE_OUT,onMouseEvent);
			view.removeEventListener(MouseEvent.MOUSE_OVER,onMouseEvent);
			view.removeEventListener(MouseEvent.MOUSE_DOWN,onMouseEvent);
		}
		
		///////////////////////////////////
		// evt handlers
		///////////////////////////////////
		
		protected function onRelease(e:MouseEvent):void 
		{
			view.gotoAndStop(1);
		}
		
		protected function onMouseEvent(e:MouseEvent):void 
		{
			switch (e.type) 
			{
				case MouseEvent.MOUSE_OUT:
				case MouseEvent.MOUSE_UP:
					view.gotoAndStop(1);
				break;
				case MouseEvent.MOUSE_OVER:
					view.gotoAndStop(2);
				break;
				case MouseEvent.MOUSE_DOWN:
					view.gotoAndStop(view.totalFrames);
				break;
				default:
			}
		}
		
		///////////////////////////////////
		// getters/setters
		///////////////////////////////////
		
		public function get label():String { return view['label'].text;}		
		public function set label(value:String):void 
		{
			view['label'].text = value;
		}
	}

}